package com.smt.modules.app.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.smt.common.enums.Code;
import com.smt.common.utils.EnumToKeyUtil;
import com.smt.common.utils.R;
import com.smt.modules.app.annotation.FileSize;
import com.smt.modules.app.annotation.Login;
import com.smt.modules.app.annotation.LoginUser;
import com.smt.modules.bus.entity.DocumentEntity;
import com.smt.modules.bus.entity.MemberUserEntity;
import com.smt.modules.bus.entity.ProjectEntity;
import com.smt.modules.bus.entity.ProjectTaskEntity;
import com.smt.modules.bus.service.DocPageService;
import com.smt.modules.bus.service.ProjectService;
import com.smt.modules.bus.service.ProjectTaskService;
import com.smt.modules.bus.vo.request.DocumentManageReq;
import com.smt.modules.bus.service.DocumentManageService;
import com.smt.modules.bus.vo.DocumentVo;
import com.smt.modules.bus.vo.request.LoadFileReq;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import java.util.List;
import java.util.Objects;


/**
 * 文件管理
 *
 * @author zhangyi
 * @date 2018/10/8 11:26
 */
@RestController
@RequestMapping("/app/doc")
@Api(tags = {"AppDocManageController"},description = "文件管理(客户端接口)")
public class AppDocManageController {

    @Autowired
    @Qualifier("documentManageService")
    private DocumentManageService documentManageService;


    @Autowired
    private DocPageService docPageService;

    /**
     * 存储文件记录
     *
     * @param documentVo
     * @return
     */
    @Login
    @PostMapping("/upload")
    @ApiOperation(value = "存储文件记录(首先使用UploadController获取路径)",httpMethod = "POST")
//    @FileSize(operate = "upload")
    public R upload(@RequestBody @Validated DocumentVo documentVo,@LoginUser MemberUserEntity entity){
        documentVo.setMemberId(entity.getUserId());
        R upload = documentManageService.upload(documentVo);
        return upload;
    }
    /**
     *  查询当前层级下的文件夹或文件
     * @param documentManageReq
     * @param userId
     * @return
     */
    @Login
    @PostMapping("/list")
    @ApiOperation(value = "获取当前层级下的文件夹和文件（根级层次不传id，查看其他子级时需要传入当前的目录id）我的文件",httpMethod = "POST")
    public R list(@RequestBody @Validated DocumentManageReq documentManageReq,
                  @RequestAttribute("userId")Long userId){
        R list = documentManageService.list(documentManageReq,userId);
        return list;
    }

    /**
     *  下载文件
     * @param fileId
     * @return
     */
    @GetMapping("/download/{fileId}")
    @ApiOperation(value = "下载文件",httpMethod = "GET")
    public R download(@PathVariable(value = "fileId") @NotEmpty(message = "文件id不能为空！") Long fileId){
        return documentManageService.download(fileId);
    }
    /**
     *  更新文件
     *
     * @param file
     * @param documentManageReq
     * @return
     */
    @Login
    @PostMapping("/update")
    @ApiOperation(value = "更新文件（传入当前的文件路径，和当前指定的文件相关信息）",httpMethod = "POST")
//    @FileSize(operate = "update")
    public R update(String file,
                    @RequestBody  @Validated DocumentManageReq documentManageReq,
                    @LoginUser MemberUserEntity memberUserEntity){
        return documentManageService.update(file,documentManageReq,memberUserEntity);
    }


    /**
     *  删除文件
     *
     * @param fileId
     * @return
     */
    @DeleteMapping("/delete/{fileId}")
    @ApiOperation(value = "删除文件(需要拿到文件id)",httpMethod = "DELETE")
//    @FileSize(operate = "delete")
    public R delete(@PathVariable(value = "fileId") @NotEmpty(message = "文件id不能为空！") Long fileId){
        return documentManageService.delete(fileId);
    }

    /**
     *  文件模糊查询
     *
     * @param fileName
     * @return
     */
    @GetMapping("/search/{projectId}/{fileName}")
    @ApiOperation(value = "文件模糊查询",httpMethod = "GET")
    public R search(@PathVariable("projectId") long projectId,
                    @PathVariable("fileName")@NotEmpty(message = "文件名不能为空") String fileName){
        return documentManageService.search(projectId,fileName);
    }


    /**
     *  修改文件夹名称
     *
     * @param dirId
     * @param dirName
     * @return
     */
    @PostMapping("/editDir/{dirId}")
    @ApiOperation(value = "修改文件夹名称",httpMethod = "POST")
    public R editDir(@PathVariable("dirId") Long dirId,
                     @RequestParam("dirName") String dirName){
        DocumentEntity documentEntity = new DocumentEntity();
        documentEntity.setId(dirId);
        documentEntity.setName(dirName);
        boolean b = documentManageService.updateById(documentEntity);
        return b?R.ok("文件夹修改成功").put("code",1):R.error("文件夹修改失败").put("code",0);
    }



    /**
     *  任务，子任务文件路径打包
     *
     * @param loadFileReq
     * @return
     */
    @Login
    @PostMapping("/docBackPage")
    @ApiOperation(value = "任务，子任务文件路径打包",httpMethod = "POST")
    public R docBackPage(@RequestBody List<LoadFileReq> loadFileReq,
                         @LoginUser MemberUserEntity memberUserEntity){
        //判断子任务和任务是否缺失了对应关系
        return docPageService.docBackPage(loadFileReq,memberUserEntity);
    }

    /**
     *  返回任务打包文件列表
     *
     * @param loadFileReq
     * @return
     */
    @Login
    @PostMapping("/docBackPageList")
    @ApiOperation(value = "返回任务打包文件列表",httpMethod = "POST")
    public R docBackPageList(@RequestBody LoadFileReq loadFileReq,
                         @LoginUser MemberUserEntity memberUserEntity){

        return docPageService.docBackPageList(loadFileReq, memberUserEntity);
    }

    /**
     *  查看详细打包文件列表
     *
     * @param id
     * @return
     */
    @Login
    @GetMapping("/docBackPageInfo/{id}")
    @ApiOperation(value = "查看详细打包文件列表",httpMethod = "GET")
    public R docBackPageInfo(@PathVariable("id") Long id,
                             @LoginUser MemberUserEntity memberUserEntity){

        return docPageService.docBackPageInfo(id, memberUserEntity);
    }
    /**
     *  删除打包后的文件
     *
     * @param id
     * @return
     */
    @Login
    @DeleteMapping("/deleteDocBackPage/{projectId}/{batchId}")
    @ApiOperation(value = "删除打包后的文件",httpMethod = "DELETE")
    public R deleteDocBackPage(@PathVariable("batchId") Long id,
                             @PathVariable("projectId") Long projectId,
                             @RequestAttribute("userId") Long userId){

        return docPageService.deleteDocBackPage(id, projectId, userId);
    }


    /**
     *  权限判断
     *
     * @param projectId
     * @return
     */
    @Login
    @PutMapping("/docBackPermission/{projectId}")
    @ApiOperation(value = "权限判断",httpMethod = "PUT")
    public R docBackPermission(@PathVariable("projectId") Long projectId,
                               @RequestAttribute("userId") Long userId){
        return docPageService.docBackPermission(projectId, userId);
    }
}
